package se.zens.demo.wsapp.implementation.customers;

import java.util.Map;

import javax.ws.rs.core.Response;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import se.zens.demo.wsapp.configuration.WSAppConfiguration;
import se.zens.demo.wsapp.implementation.AbstractWSAppProcessor;
import se.zens.demo.wsapp.interfaces.model.Customer;

public class AddCustomerProcessor extends AbstractWSAppProcessor<Response>
{
	
	private static Logger logger = LogManager.getLogger(AddCustomerProcessor.class);
	private Map<String, Customer> customers;
	private Response response ;
	private Customer request;
	private WSAppConfiguration config;
	
	public AddCustomerProcessor(WSAppConfiguration config, Customer customer,
			Map<String, Customer> customers)
	{
		this.config = config;
		this.request = customer;
		this.customers = customers;
	}

	public AddCustomerProcessor(WSAppConfiguration config, String id,
			String name, Map<String, Customer> customers)
	{
		this.config = config;
		this.request = new Customer(id,name);
		this.customers = customers;
	}

	@Override
	public void process()
	{
		if(request.getId() == null || request.getId().isEmpty())
		{
			response = Response.noContent().build();
			return;
		}
		
		if(customers.get(request.getId()) != null)
		{
			response = Response.notModified().build();
			return;
		}
		
		customers.put(request.getId(), request);
		
		response = Response.ok(request).build();
		
	}
	
	public Response getResponse()
	{
		return response;
	}

}
